package my;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.sun.jdi.connect.spi.Connection;

import java.awt.FlowLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;

public class yinghan extends JFrame {

	private JPanel contentPane;
	private JTextField input;
	private JTextField output;
	Connection con;
	ResultSet rs;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					yinghan frame = new yinghan();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public yinghan() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		input = new JTextField();
		input.setBounds(69, 42, 184, 28);
		contentPane.add(input);
		input.setColumns(10);
		JButton search = new JButton("search");
		search.setBounds(277, 44, 103, 26);
		contentPane.add(search);

		JTextArea output = new JTextArea();
		output.setBounds(36, 93, 379, 160);
		contentPane.add(output);
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = input.getText();
				String regex1 = "[A-Za-z]+";
				String regex2 = "[\u4e00-\u9fa5]+";
				boolean flag = true;
				if (str.matches(regex2) || str.matches(regex1)) {
					if (str.matches(regex2)) {

						try {
							Class.forName("com.mysql.jdbc.Driver");
							String uri = "jdbc:mysql://localhost:3306/mysql";
							String user = "root";
							String password = "";
							con = (Connection) DriverManager.getConnection(uri, user, password);

							String sqlStr = "select * from fanyi  ";
							PreparedStatement pre;
							pre = ((java.sql.Connection) con).prepareStatement(sqlStr);
							pre.setString(1, "%" + str);
							rs = pre.executeQuery();
							while (rs.next()) {
								output.setText(rs.getString(2) + "\n" + rs.getString(3));
								flag = false;
							}
							if (flag == true)
								output.setText("no search");

							con.close();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					} else if (str.matches(regex1)) {
						try {
							Class.forName("com.mysql.jdbc.Driver");
							String uri = "jdbc:mysql://localhost:3306/mysql";
							String user = "root";
							String password = "";
							con = (Connection) DriverManager.getConnection(uri, user, password);

							String sqlStr = "select * from entoch where ch like ?";
							PreparedStatement pre;
							pre = ((java.sql.Connection) con).prepareStatement(sqlStr);
							pre.setString(1, "_%" + str + "_%");

							rs = pre.executeQuery(); 
							while (rs.next()) {

								output.setText(rs.getString(1) + "\n" + rs.getString(3));
								flag = false;
							}
							if (flag == true)
								output.setText("no search");
							try {
								con.close();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
						} catch (ClassNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}

					else {
						output.setText("请输入中文或者英文");
						JOptionPane.showMessageDialog(output, "格式不匹配", "消息对话框", JOptionPane.WARNING_MESSAGE);
					}

				}
			}
		});

	}
	
}
